from django.db import IntegrityError
from django.db.models.deletion import ProtectedError
from django.http import JsonResponse
from django.shortcuts import render
from utils.customerutils import CustomerUtils
from utils.responsestatus import ResponseStatus
from utils import responsedata
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from dept.models import Department

# Create your views here.
# 部门分页
def dept_page(request):
    if request.method == 'POST':
        form_data = CustomerUtils.get_request_data(request)
        dept_list = Department.objects.all().order_by('-id')
        paginator = Paginator(dept_list, form_data.get('size', 5))
        total = paginator.count
        # 当前页数据
        current_page = paginator.page(form_data.get('page', 1))
        table_data = list(current_page.object_list.values())
        response = {
            'code': ResponseStatus.BUSINESS_SUCCESS,
            'data': {
                'total': total,
                'tableData': table_data
            }
        }
        return CustomerUtils.response_json(response)

def dept_one(request, id):
    dept = Department.objects.filter(id=id).first()
    print(dept)
    response = {
        'code': ResponseStatus.BUSINESS_SUCCESS,
        'data': {
            'dept': dept
        }
    }
    return CustomerUtils.response_json(response)

def dept_list(request):
    dept_list = Department.objects.all()
    depts = []
    for dept in dept_list:
        depts.append({
            'dept_id': dept.id,
            'dept_name': dept.dept_name,
        })
    resp = {
        'code': ResponseStatus.BUSINESS_SUCCESS,
        'data': {
            'depts': depts
        }
    }
    return CustomerUtils.response_json(resp)


def dept_insert_edit(request):
    if request.method == 'POST':
        form_data = CustomerUtils.get_request_data(request)
        print(form_data)
        id = form_data.get('id')
        try:
            # 新增
            if id is None:
                Department(**form_data).save()
                return CustomerUtils.response_json(responsedata.BUSINESS_SUCCESS_DATA)
            # 修改
            Department.objects.filter(id=id).update(**form_data)
            return CustomerUtils.response_json(responsedata.BUSINESS_SUCCESS_DATA)
        except IntegrityError as e:
            # 想让错误提示更细致
            result = responsedata.BUSINESS_ERROR_DATA
            result['message'] = str(e)
            return CustomerUtils.response_json(result)


def dept_batch_remove(request):
    if request.method == 'POST':
        form_data = CustomerUtils.get_request_data(request)
        ids = form_data.get('ids')
        try:
            Department.objects.filter(id__in=ids).delete()
            return CustomerUtils.response_json(responsedata.BUSINESS_SUCCESS_DATA)
        except ProtectedError as e:
            response = responsedata.BUSINESS_ERROR_DATA
            response['message'] = "部门被占用，不能删除！"
            return CustomerUtils.response_json(response)















